package com.sdsy.blockchaintrainingback.service.user;

import cn.hutool.core.bean.BeanUtil;
import com.sdsy.blockchaintrainingback.context.ThreadLocalContextHolder;
import com.sdsy.blockchaintrainingback.mapper.user.UserMapper;
import com.sdsy.blockchaintrainingback.pojo.user.User;
import com.sdsy.blockchaintrainingback.service.user.UserService;
import com.sdsy.blockchaintrainingback.vo.user.UserQueryVo;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


/**
 * @Description TODO
 * @Author linzili
 * @Date 2023/9/24 22:30
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 通过用户名查询用户
     *
     * @param username
     * @return
     */
    @Override
    public User getUserByUsername(String username) {
        return userMapper.selectByUsername(username);
    }


    /**
     * 判断密码是否匹配
     *
     * @param rawPassword     未加密的密码
     * @param encodedPassword 加密后的密码
     * @return 是否匹配
     */
    @Override
    public boolean isPasswordMatch(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder.matches(rawPassword, encodedPassword);
    }

    @Override
    public UserQueryVo getUserProfile() {
        Long userId = ThreadLocalContextHolder.getUserId();
        User userDO = userMapper.selectById(userId);
        UserQueryVo userProfileVo = new UserQueryVo();
        BeanUtil.copyProperties(userDO,userProfileVo);
        return userProfileVo;
    }
}
